/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kpsmart.gui;

import java.awt.event.ActionListener;
import java.util.Collection;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author klaus
 */
public class PriceForm extends javax.swing.JFrame {

    /**
     * Creates new form PriceForm
     */
    PriceForm() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel11 = new javax.swing.JLabel();
        jButtonCancel = new javax.swing.JButton();
        jButtonSubmit = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jFormattedTextFieldWeightcost = new javax.swing.JFormattedTextField();
        jFormattedTextFieldVolumecost = new javax.swing.JFormattedTextField();
        jTextFieldTo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldFrom = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jCheckBoxTo = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Price Update");
        setAlwaysOnTop(true);
        setResizable(false);

        jLabel11.setText("Volumecost");

        jButtonCancel.setText("Cancel");

        jButtonSubmit.setText("Submit");

        jLabel6.setText("Priority");

        jFormattedTextFieldWeightcost.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jFormattedTextFieldWeightcost.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jFormattedTextFieldWeightcost.setText("0");

        jFormattedTextFieldVolumecost.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        jFormattedTextFieldVolumecost.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jFormattedTextFieldVolumecost.setText("0");

        jTextFieldTo.setEditable(false);
        jTextFieldTo.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel5.setText("To");

        jTextFieldFrom.setEditable(false);
        jTextFieldFrom.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTextFieldFrom.setText("New Zealand");

        jLabel4.setText("From");

        jLabel12.setText("Weightcost");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jCheckBoxTo.setSelected(true);
        jCheckBoxTo.setText("New Zealand");
        jCheckBoxTo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxToActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonSubmit)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonCancel))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jFormattedTextFieldVolumecost, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jCheckBoxTo)
                                    .addGap(18, 18, 18)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jTextFieldTo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
                                        .addComponent(jTextFieldFrom, javax.swing.GroupLayout.Alignment.TRAILING)))
                                .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 339, Short.MAX_VALUE)
                                .addComponent(jFormattedTextFieldWeightcost, javax.swing.GroupLayout.Alignment.TRAILING)))))
                .addGap(13, 13, 13))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTextFieldFrom, jTextFieldTo});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBoxTo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jFormattedTextFieldVolumecost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jFormattedTextFieldWeightcost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSubmit)
                    .addComponent(jButtonCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jComboBox1, jFormattedTextFieldVolumecost, jFormattedTextFieldWeightcost, jTextFieldFrom, jTextFieldTo});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCheckBoxToActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxToActionPerformed
        this.jTextFieldTo.setText("");
        if (!this.jCheckBoxTo.isSelected()) {
            this.jTextFieldTo.setEditable(true);
        } else {
            this.jTextFieldTo.setEditable(false);
        }
    }//GEN-LAST:event_jCheckBoxToActionPerformed

    /**
     * Add ActionListener to cancel button
     *
     * @param l
     */
    void addListenerToCancel(ActionListener l) {
        this.jButtonCancel.addActionListener(l);
    }

    /**
     * Add ActionListener to submit button
     *
     * @param l
     */
    void addListenerToSubmit(ActionListener l) {
        this.jButtonSubmit.addActionListener(l);
    }

    /**
     * Set values of priority combo box
     *
     * @param priorities
     */
    void setPriorities(Collection<String> priorities) {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (String s : priorities) {
            model.addElement(s);
        }
        this.jComboBox1.setModel(model);
    }

    /**
     * Get value of from-field String
     *
     * @return from String
     */
    String getFrom() {
        return this.jTextFieldFrom.getText();
    }

    /**
     * Get value of to-field String
     *
     * @return to String
     */
    String getTo() {
        if (this.jCheckBoxTo.isSelected()) {
            return "New Zealand";
        } else {
            return this.jTextFieldTo.getText();
        }
    }

    /**
     * Get value of priority combo box as String
     *
     * @return priority String
     */
    String getPriority() {
        return this.jComboBox1.getSelectedItem().toString();
    }

    /**
     * Get volumecost as int
     *
     * @return volumecost int
     */
    int getVolumecost() {
        return Integer.valueOf(this.jFormattedTextFieldVolumecost.getText());
    }

    /**
     * Get weightcost as int
     *
     * @return weightcost int
     */
    int getWeightcost() {
        return Integer.valueOf(this.jFormattedTextFieldWeightcost.getText());
    }

    /**
     * Check if the field volumecost has an integer value
     *
     * @return proper boolean
     */
    boolean properVolumecost() {
        return isProperInteger(this.jFormattedTextFieldVolumecost.getText());
    }

    /**
     * Check if the field weightcost has an integer value
     *
     * @return proper boolean
     */
    boolean properWeightcost() {
        return isProperInteger(this.jFormattedTextFieldWeightcost.getText());
    }

    /**
     * Check if a String has an integer value
     *
     * @return proper boolean
     */
    private boolean isProperInteger(String string) {
        try {
            Integer.valueOf(string);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonSubmit;
    private javax.swing.JCheckBox jCheckBoxTo;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JFormattedTextField jFormattedTextFieldVolumecost;
    private javax.swing.JFormattedTextField jFormattedTextFieldWeightcost;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jTextFieldFrom;
    private javax.swing.JTextField jTextFieldTo;
    // End of variables declaration//GEN-END:variables
}
